package com.basic.technology.study.introduction.thread.ThreadLocal;

public class ThreadLocalTest {

    ThreadLocal<Long> longThreadLocal = new ThreadLocal<>(){
        @Override
        protected Long initialValue() {
            return Thread.currentThread().getId();
        }
    };
    ThreadLocal<String> strThreadLocal = new ThreadLocal<>(){
        @Override
        protected String initialValue() {
            return Thread.currentThread().getName();
        }
    };

    public void set(){
        longThreadLocal.set(Thread.currentThread().getId());
        strThreadLocal.set(Thread.currentThread().getName());
    }

    public long getLong(){
        return longThreadLocal.get();
    }

    public String getString(){
        return strThreadLocal.get();
    }

    public static void main(String[] args) throws InterruptedException {

        final ThreadLocalTest test = new ThreadLocalTest();
        //test.set();
        System.out.println(test.getLong());
        System.out.println(test.getString());

        Thread t1 = new Thread(){
            @Override
            public void run() {
                //test.set();
                System.out.println(test.getLong());
                System.out.println(test.getString());
            }
        };
        t1.start();
        t1.join();

        Thread t2 = new Thread(){
            @Override
            public void run() {
                //test.set();
                System.out.println(test.getLong());
                System.out.println(test.getString());
            }
        };
        t2.start();
        t2.join();
        System.out.println(test.getLong());
        System.out.println(test.getString());
    }
}
